package com.thread.book.book2;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class Book06 {

    private static final ThreadLocal<Long> TIME_THREADLOCAL = new ThreadLocal<Long>() {
        protected Long initialValue() {
            return System.currentTimeMillis();
        }
    };

    public static final void begin() {
        TIME_THREADLOCAL.set(System.currentTimeMillis());
    }

    public static final long end() {
        // 第一次get()方法调用时会进行初始化（如果set方法没有调用），每个线程会调用一次
        return System.currentTimeMillis() - TIME_THREADLOCAL.get();
    }

    //计算程序运行时间
    public static void main(String[] args) throws Exception {
        Book06.begin();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("Cost: " + Book06.end() + " mills");
    }
}
